﻿/*
 * ======================================================================
 *                           Modern Warfare3 Launcher
 * Copyright 2011-2012
 * Developer:Pooya parsa
 * Email:pingsft@gmail.com
 * You are ALLOWED:
 * -Read source code
 * -Use some part of source code with Full name and email of developer
 * -Release your own mod with name of develepor
 * You are NOT ALLOWD:
 * -Add cracked version of Mw3
 * -Release modified version as MW3Launcher
 * -Mirror Sourcecode
 * -Use source code without name of developer
 * Thank you for your attention
 * ======================================================================
 * */
namespace MW3Launcher
{
    using System;
    using System.Runtime.CompilerServices;
    using System.Runtime.InteropServices;

    [StructLayout(LayoutKind.Sequential)]
    public struct STARTUPINFO
    {
        public uint cb;
        public string lpReserved;
        public string lpDesktop;
        public string lpTitle;
        public uint dwX;
        public uint dwY;
        public uint dwXSize;
        public uint dwYSize;
        public uint dwXCountChars;
        public uint dwYCountChars;
        public uint dwFillAttribute;
        public uint dwFlags;
        public short wShowWindow;
        public short cbReserved2;
        public IntPtr lpReserved2;
        public IntPtr hStdInput;
        public IntPtr hStdOutput;
        public IntPtr hStdError;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct CONTEXT
    {
        public uint ContextFlags;
        public uint Dr0;
        public uint Dr1;
        public uint Dr2;
        public uint Dr3;
        public uint Dr6;
        public uint Dr7;
        public FLOATING_SAVE_AREA FloatSave;
        public uint SegGs;
        public uint SegFs;
        public uint SegEs;
        public uint SegDs;
        public uint Edi;
        public uint Esi;
        public uint Ebx;
        public uint Edx;
        public uint Ecx;
        public uint Eax;
        public uint Ebp;
        public uint Eip;
        public uint SegCs;
        public uint EFlags;
        public uint Esp;
        public uint SegSs;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x200)]
        public byte[] ExtendedRegisters;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct FLOATING_SAVE_AREA
    {
        public uint ControlWord;
        public uint StatusWord;
        public uint TagWord;
        public uint ErrorOffset;
        public uint ErrorSelector;
        public uint DataOffset;
        public uint DataSelector;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 80)]
        public byte[] RegisterArea;
        public uint Cr0NpxState;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct PROCESS_INFORMATION
    {
        public IntPtr hProcess;
        public IntPtr hThread;
        public uint dwProcessId;
        public uint dwThreadId;
    }

    public static class Win32Apis
    {
        public const int CERT_QUERY_CONTENT_CERT = 1;
        public const int CERT_QUERY_CONTENT_CERT_PAIR = 13;
        public const int CERT_QUERY_CONTENT_CRL = 3;
        public const int CERT_QUERY_CONTENT_CTL = 2;
        public const int CERT_QUERY_CONTENT_FLAG_ALL = 0x3ffe;
        public const int CERT_QUERY_CONTENT_FLAG_CERT = 2;
        public const int CERT_QUERY_CONTENT_FLAG_CERT_PAIR = 0x2000;
        public const int CERT_QUERY_CONTENT_FLAG_CRL = 8;
        public const int CERT_QUERY_CONTENT_FLAG_CTL = 4;
        public const int CERT_QUERY_CONTENT_FLAG_PFX = 0x1000;
        public const int CERT_QUERY_CONTENT_FLAG_PKCS10 = 0x800;
        public const int CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED = 0x100;
        public const int CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED_EMBED = 0x400;
        public const int CERT_QUERY_CONTENT_FLAG_PKCS7_UNSIGNED = 0x200;
        public const int CERT_QUERY_CONTENT_FLAG_SERIALIZED_CERT = 0x20;
        public const int CERT_QUERY_CONTENT_FLAG_SERIALIZED_CRL = 0x80;
        public const int CERT_QUERY_CONTENT_FLAG_SERIALIZED_CTL = 0x40;
        public const int CERT_QUERY_CONTENT_FLAG_SERIALIZED_STORE = 0x10;
        public const int CERT_QUERY_CONTENT_PFX = 12;
        public const int CERT_QUERY_CONTENT_PKCS10 = 11;
        public const int CERT_QUERY_CONTENT_PKCS7_SIGNED = 8;
        public const int CERT_QUERY_CONTENT_PKCS7_SIGNED_EMBED = 10;
        public const int CERT_QUERY_CONTENT_PKCS7_UNSIGNED = 9;
        public const int CERT_QUERY_CONTENT_SERIALIZED_CERT = 5;
        public const int CERT_QUERY_CONTENT_SERIALIZED_CRL = 7;
        public const int CERT_QUERY_CONTENT_SERIALIZED_CTL = 6;
        public const int CERT_QUERY_CONTENT_SERIALIZED_STORE = 4;
        public const int CERT_QUERY_FORMAT_ASN_ASCII_HEX_ENCODED = 3;
        public const int CERT_QUERY_FORMAT_BASE64_ENCODED = 2;
        public const int CERT_QUERY_FORMAT_BINARY = 1;
        public const int CERT_QUERY_FORMAT_FLAG_ALL = 14;
        public const int CERT_QUERY_FORMAT_FLAG_ASN_ASCII_HEX_ENCODED = 8;
        public const int CERT_QUERY_FORMAT_FLAG_BASE64_ENCODED = 4;
        public const int CERT_QUERY_FORMAT_FLAG_BINARY = 2;
        public const int CERT_QUERY_OBJECT_BLOB = 2;
        public const int CERT_QUERY_OBJECT_FILE = 1;
        public static UIntPtr CMS_SIGNER_INFO = new UIntPtr(0x1f5);
        public const int CMSG_ATTR_CERT_COUNT_PARAM = 0x1f;
        public const int CMSG_ATTR_CERT_PARAM = 0x20;
        public const int CMSG_BARE_CONTENT_PARAM = 3;
        public const int CMSG_CERT_COUNT_PARAM = 11;
        public const int CMSG_CERT_PARAM = 12;
        public const int CMSG_CMS_RECIPIENT_COUNT_PARAM = 0x21;
        public const int CMSG_CMS_RECIPIENT_ENCRYPTED_KEY_INDEX_PARAM = 0x23;
        public const int CMSG_CMS_RECIPIENT_INDEX_PARAM = 0x22;
        public const int CMSG_CMS_RECIPIENT_INFO_PARAM = 0x24;
        public const int CMSG_CMS_SIGNER_INFO_PARAM = 0x27;
        public const int CMSG_COMPUTED_HASH_PARAM = 0x16;
        public const int CMSG_CONTENT_PARAM = 2;
        public const int CMSG_CRL_COUNT_PARAM = 13;
        public const int CMSG_CRL_PARAM = 14;
        public const int CMSG_ENCODED_MESSAGE = 0x1d;
        public const int CMSG_ENCODED_SIGNER = 0x1c;
        public const int CMSG_ENCRYPT_PARAM = 0x1a;
        public const int CMSG_ENCRYPTED_DIGEST = 0x1b;
        public const int CMSG_ENVELOPE_ALGORITHM_PARAM = 15;
        public const int CMSG_HASH_ALGORITHM_PARAM = 20;
        public const int CMSG_HASH_DATA_PARAM = 0x15;
        public const int CMSG_INNER_CONTENT_TYPE_PARAM = 4;
        public const int CMSG_RECIPIENT_COUNT_PARAM = 0x11;
        public const int CMSG_RECIPIENT_INDEX_PARAM = 0x12;
        public const int CMSG_RECIPIENT_INFO_PARAM = 0x13;
        public const int CMSG_SIGNER_AUTH_ATTR_PARAM = 9;
        public const int CMSG_SIGNER_CERT_ID_PARAM = 0x26;
        public const int CMSG_SIGNER_CERT_INFO_PARAM = 7;
        public const int CMSG_SIGNER_COUNT_PARAM = 5;
        public const int CMSG_SIGNER_HASH_ALGORITHM_PARAM = 8;
        public const int CMSG_SIGNER_INFO_PARAM = 6;
        public const int CMSG_SIGNER_UNAUTH_ATTR_PARAM = 10;
        public const int CMSG_TYPE_PARAM = 1;
        public const int CMSG_UNPROTECTED_ATTR_PARAM = 0x25;
        public const int CMSG_VERSION_PARAM = 30;
        public const int CRYPT_ASN_ENCODING = 1;
        public const int CRYPT_NDR_ENCODING = 2;
        public const int PKCS_7_ASN_ENCODING = 0x10000;
        public const int PKCS_7_NDR_ENCODING = 0x20000;
        public static UIntPtr PKCS7_SIGNER_INFO = new UIntPtr(500);
        public static string szOID_RSA_counterSign = "1.2.840.113549.1.9.6";
        public static string szOID_RSA_signingTime = "1.2.840.113549.1.9.5";
        public const int X509_ASN_ENCODING = 1;
        public const int X509_NDR_ENCODING = 2;

        [return: MarshalAs(UnmanagedType.Bool)]
        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern bool CloseHandle(IntPtr hObject);
        [DllImport("kernel32.dll")]
        public static extern bool CreateProcess(string lpApplicationName, string lpCommandLine, IntPtr lpProcessAttributes, IntPtr lpThreadAttributes, bool bInheritHandles, uint dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory, ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation);
        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttribute, IntPtr dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);
        [return: MarshalAs(UnmanagedType.Bool)]
        [DllImport("crypt32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern bool CryptDecodeObject(uint CertEncodingType, UIntPtr lpszStructType, byte[] pbEncoded, uint cbEncoded, uint flags, [In, Out] byte[] pvStructInfo, ref uint cbStructInfo);
        [DllImport("crypt32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern bool CryptMsgGetParam(IntPtr hCryptMsg, int dwParamType, int dwIndex, [In, Out] byte[] vData, ref int pcbData);
        [DllImport("crypt32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern bool CryptMsgGetParam(IntPtr hCryptMsg, int dwParamType, int dwIndex, IntPtr pvData, ref int pcbData);
        [DllImport("crypt32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern bool CryptQueryObject(int dwObjectType, IntPtr pvObject, int dwExpectedContentTypeFlags, int dwExpectedFormatTypeFlags, int dwFlags, out int pdwMsgAndCertEncodingType, out int pdwContentType, out int pdwFormatType, ref IntPtr phCertStore, ref IntPtr phMsg, ref IntPtr ppvContext);
        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr GetModuleHandle(string lpModuleName);
        [DllImport("kernel32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern UIntPtr GetProcAddress(IntPtr hModule, string procName);
        [DllImport("kernel32.dll")]
        public static extern bool GetThreadContext(IntPtr hThread, ref CONTEXT lpContext);
        [DllImport("kernel32.dll")]
        public static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId);
        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, [Out] byte[] lpBuffer, int dwSize, out int lpNumberOfBytesRead);
        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, IntPtr lpBuffer, int dwSize, out int lpNumberOfBytesRead);
        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, [Out, MarshalAs(UnmanagedType.AsAny)] object lpBuffer, int dwSize, out int lpNumberOfBytesRead);
        [DllImport("kernel32.dll")]
        public static extern uint ResumeThread(IntPtr hThread);
        [return: MarshalAs(UnmanagedType.Bool)]
        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern bool TerminateProcess(IntPtr hProcess, uint uExitCode);
        [DllImport("kernel32.dll")]
        public static extern bool TerminateThread(IntPtr hThread, uint dwExitCode);
        [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
        public static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, AllocationType flAllocationType, MemoryProtection flProtect);
        [DllImport("kernel32.dll")]
        public static extern bool VirtualProtectEx(IntPtr hProcess, IntPtr lpAddress, UIntPtr dwSize, uint flNewProtect, out uint lpflOldProtect);
        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, uint nSize, out UIntPtr lpNumberOfBytesWritten);

        [Flags]
        public enum AllocationType : uint
        {
            COMMIT = 0x1000,
            LARGE_PAGES = 0x20000000,
            PHYSICAL = 0x400000,
            RESERVE = 0x2000,
            RESET = 0x80000,
            TOP_DOWN = 0x100000,
            WRITE_WATCH = 0x200000
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct BLOB
        {
            public int cbData;
            public IntPtr pbData;
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct CERT_ID
        {
            public int dwIdChoice;
            public Win32Apis.BLOB IssuerSerialNumberOrKeyIdOrHashId;
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct CERT_NAME_BLOB
        {
            public uint cbData;
            [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)]
            public byte[] pbData;
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct CMSG_SIGNER_INFO
        {
            public int dwVersion;
            private Win32Apis.CERT_NAME_BLOB Issuer;
            private Win32Apis.CRYPT_INTEGER_BLOB SerialNumber;
            private Win32Apis.CRYPT_ALGORITHM_IDENTIFIER HashAlgorithm;
            private Win32Apis.CRYPT_ALGORITHM_IDENTIFIER HashEncryptionAlgorithm;
            private Win32Apis.BLOB EncryptedHash;
            private Win32Apis.CRYPT_ATTRIBUTE[] AuthAttrs;
            private Win32Apis.CRYPT_ATTRIBUTE[] UnauthAttrs;
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct CRYPT_ALGORITHM_IDENTIFIER
        {
            public string pszObjId;
            private Win32Apis.BLOB Parameters;
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct CRYPT_ATTR_BLOB
        {
            public uint cbData;
            [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)]
            public byte[] pbData;
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct CRYPT_ATTRIBUTE
        {
            [MarshalAs(UnmanagedType.LPStr)]
            public string pszObjId;
            public uint cValue;
            [MarshalAs(UnmanagedType.LPStruct)]
            public Win32Apis.CRYPT_ATTR_BLOB rgValue;
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct CRYPT_INTEGER_BLOB
        {
            public uint cbData;
            public IntPtr pbData;
        }

        [Flags]
        public enum MemoryProtection : uint
        {
            EXECUTE = 0x10,
            EXECUTE_READ = 0x20,
            EXECUTE_READWRITE = 0x40,
            EXECUTE_WRITECOPY = 0x80,
            GUARD_Modifierflag = 0x100,
            NOACCESS = 1,
            NOCACHE_Modifierflag = 0x200,
            READONLY = 2,
            READWRITE = 4,
            WRITECOMBINE_Modifierflag = 0x400,
            WRITECOPY = 8
        }

        public enum Protection
        {
            PAGE_EXECUTE = 0x10,
            PAGE_EXECUTE_READ = 0x20,
            PAGE_EXECUTE_READWRITE = 0x40,
            PAGE_EXECUTE_WRITECOPY = 0x80,
            PAGE_GUARD = 0x100,
            PAGE_NOACCESS = 1,
            PAGE_NOCACHE = 0x200,
            PAGE_READONLY = 2,
            PAGE_READWRITE = 4,
            PAGE_WRITECOMBINE = 0x400,
            PAGE_WRITECOPY = 8
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct SIGNER_SUBJECT_INFO
        {
            public uint cbSize;
            public IntPtr pdwIndex;
            public uint dwSubjectChoice;
            public Win32Apis.SubjectChoiceUnion Union1;
        }

        [StructLayout(LayoutKind.Explicit)]
        public struct SubjectChoiceUnion
        {
            [FieldOffset(0)]
            public IntPtr pSignerBlobInfo;
            [FieldOffset(0)]
            public IntPtr pSignerFileInfo;
        }

        public delegate int ThreadProc(IntPtr param);
    }


}

